package com.niit.order.web;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.niit.order.pojo.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;


/**
 * 请求合并案例
 * @author yaojianfeng
 */
@RestController
@RequestMapping("/consumer")
public class HystrixConsumerController04 {
    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("/findUser/{id}")
    @HystrixCollapser(
            batchMethod = "findAll", collapserProperties = {
            @HystrixProperty(name = "timerDelayInMilliseconds", value = "10")
    })
    public User findUser(@PathVariable("id") String id) {
        return null;
    }

    @HystrixCommand
    public List<User> findAll(List<String> ids) {
        User[] forObject = restTemplate.getForObject("http://userservice/user/findAll/{1}", User[].class, StringUtils.join(ids, ","));
        assert forObject != null;
        return Arrays.asList(forObject);
    }

}
